package com.heima.article.listener;

import com.heima.article.service.IApHotArticlesService;
import com.heima.common.constants.message.HotArticleConstants;
import com.heima.common.util.JsonUtils;
import com.heima.model.article.msg.ArticleBehaviorStreamMsg;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class HotArticleScoreConsumerListener {

    @Autowired
    private IApHotArticlesService hotArticlesService;

    @KafkaListener(topics = HotArticleConstants.HOT_ARTICLE_SCORE_CONSUMER_TOPIC)
    public void receiveMsg(ConsumerRecord<String,String> record){

        Optional<ConsumerRecord<String, String>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){
            String value = record.value();
            if(StringUtils.isNotBlank(value)){
                ArticleBehaviorStreamMsg streamMsg = JsonUtils.toBean(value, ArticleBehaviorStreamMsg.class);
//                更新文章分值
                hotArticlesService.updateHotArticle(streamMsg);
            }
        }
    }
}
